package com.zvin.testndk;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.Log;

import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
 * Created by chenzhengwen on 2015/6/25.
 */
public class Utils {
    public static final String TAG = "Utils";
    public static String execCommand(String command){
        StringBuilder resultBuilder = new StringBuilder();
        Process process = null;
        try{
            process = Runtime.getRuntime().exec(new String[]{"sh", "-c", command});
        }catch (Exception e){
            Log.e(TAG, e.getMessage());
            return resultBuilder.toString();
        }

        int result = -1;
        try{
            result = process.waitFor();
        }catch (Exception e){
            Log.e(TAG, e.getMessage());
            return resultBuilder.toString();
        }

        //error execute command
        if(result != 0){
            String line = null;
            BufferedReader reader = null;
            try{
                reader = new BufferedReader(new InputStreamReader(process.getErrorStream()));

                while((line = reader.readLine()) != null){
                    resultBuilder.append(line);
                }
            }catch (Exception e){
                Log.e(TAG, e.getMessage());
                return resultBuilder.toString();
            }finally {
                if(reader != null){
                    try {
                        reader.close();
                    }catch (Exception e){
                        Log.e(TAG, e.getMessage());
                    }
                }
            }
            return resultBuilder.toString();
        }

        //command execute is ok
        String line = null;
        BufferedReader buffReader = null;
        try{
            buffReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            while ((line = buffReader.readLine()) != null){
                resultBuilder.append(line);
            }
        }catch (Exception e){
            Log.e(TAG, e.getMessage());
        }finally {
            if(buffReader != null){
                try{
                    buffReader.close();
                }catch (Exception e){
                    Log.e(TAG, e.getMessage());
                }
            }
        }

        return resultBuilder.toString();
    }

    /*
    *获取 ActionBar 的高度
    * */
    public static int getActionBarSize(Context context) {

        TypedArray actionbarSizeTypedArray = context.obtainStyledAttributes(new int[]{

                android.R.attr.actionBarSize

        });

        return actionbarSizeTypedArray.getDimensionPixelSize(0,0);
    }

    /*
    * 获取状态栏的高度
    * */
    public static int getStatusBarHeight(Context context){
        int height = 0;
        int resId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        height = context.getResources().getDimensionPixelSize(resId);

        return height;
    }
}
